window.onload=function () {
    const E = window.wangEditor
    const editor = new E('#div1')
    const text = $('#text')

    // 监控变化，同步更新到 textarea
    editor.config.onchange = function (html) {
        text.val(html)
    }
    // 配置 server 接口地址
    editor.config.uploadImgServer = 'upload-img.do';
    // 配置alt选项
    editor.config.showLinkImgAlt = false;
    // 配置超链接
    editor.config.showLinkImgHref = false;
    editor.create();

    //获取当前选择下拉框中的所有值
    var vals = new Array();
    // var texts = new Array();
    function newBuildReturnValue(selectName){
        $("#"+selectName+" option").each(function(){
            //遍历所有option
            var value = $(this).val();   //获取option值
            // var text = $(this).text();
            vals.push(value);
            // texts.push("2");
        });
    }
    newBuildReturnValue("selectType");

    $.ajax({
        type:"get",
        url:"viewPost.do",
        dataType:"text",
        data:{pid:args.pid},
        success:function(back){
            console.log(back);
            var obj = eval( "(" + back + ")" );//转换后的JSON对象
            //选择下拉框动态选择
            $(" #selectType option[value='"+obj.post.ptype+"']").attr("selected","selected");
            document.getElementById("title").innerHTML += obj.post.ptitle;
            // 将获取到的内容放入Editor中
            editor.txt.html(obj.post.pcontent);
            
            //记得还要获取用户id

            //修改页面title
            $(document).attr('title',obj.ptitle);
        },
        error:function(){
            console.log("请求访问帖子数据失败");
        }
    });

    //获取当前时间
    function getCurrentTime(){
        let now= new Date();
        let _month = ( 10 > (now.getMonth()+1) ) ? '0' + (now.getMonth()+1) : now.getMonth()+1;
        let _day = ( 10 > now.getDate() ) ? '0' + now.getDate() : now.getDate();
        let _hour = ( 10 > now.getHours() ) ? '0' + now.getHours() : now.getHours();
        let _minute = ( 10 > now.getMinutes() ) ? '0' + now.getMinutes() : now.getMinutes();
        let _second = ( 10 > now.getSeconds() ) ? '0' + now.getSeconds() : now.getSeconds();
        return now.getFullYear() + '-' + _month + '-' + _day + ' ' + _hour + ':' + _minute + ':' + _second;
    }
    //发表帖子
    $("#submit").click(function () {
        var type = $("#selectType").val();
        var title = $("#title").val();
        var content = $("#text").val();
        var time = getCurrentTime();
        $.post('updatePost.do',{type:type,title:title,content:content,time:time,pstatus:1},function (back) {
            console.log(back);
        });
    });

    //帖子存为草稿
    $("#draft").click(function () {
        var type = $("#selectType").val();
        var title = $("#title").val();
        var content = $("#text").val();
        var time = getCurrentTime();
        $.post('updatePost.do',{type:type,title:title,content:content,time:time,pstatus:0},function (back) {
            console.log(back);
        });
    });
}